﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.SystemHelp
{
    public partial class Login : Form
    {
        public Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass();
        public static int LogUserId = 1000;
        public static string LogUserName = "";
        public static string LogTrueName = "";
        public static int CustomID = 0;
        public static string CustomerCode = "";
        public static string CustomerName = "";
        public static string RoleName = "";
        /// <summary>
        /// 是否是免费使用用户
        /// </summary>
        public static bool IsTestUser = false;
        public static string TestUserMessage = "请用正式版 ，QQ:：539217021，好用又便宜的在正式版！";

        public Login()
        {
            InitializeComponent();
            menuBlueButton1.BringToFront();
        }

        private void menuBlueButton1_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text;
            if (string.IsNullOrEmpty(userName))
            {
                PageCommon.Alert("用户名不能为空！");
                return;
            }
            string password = txtPassword.Text;
            if (string.IsNullOrEmpty(password))
            {
                PageCommon.Alert("密码不能为空！");
                return;
            }
            CheckLogin(userName, password);
        }

        private void CheckLogin(string userName, string password)
        {
            //Aliyeye.Car.Net.UI.MainWin main = new MainWin();
            //main.Tag = this.FindForm();
            //this.Visible = false;
            //MainWin.GetInstance();
            //main.ShowDialog();

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            AccountsUsersSvc.Accounts_Users model = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_Users();
            ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
                proxy => proxy.CheckLogin(sopaHeader, userName, password),
                result =>
                {
                    if (result.Error == null)
                    {
                        LogUserId = Convert.ToInt32(result.Result);

                        if (LogUserId == 0)
                        {
                            PageCommon.Alert("登录失败，请重新登录！", this);
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            return;
                        }

                        AccountsUsersSvc.Accounts_UsersServiceSoapClient client = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_UsersServiceSoapClient();
                        model = client.GetModel(sopaHeader, LogUserId);
                        LogUserName = model.UserName;
                        LogTrueName = model.TrueName;
                        CustomID = model.CustomID;
                        CustomerCode = model.CustomerCode;
                        CustomerName = model.CustomerName;
                        RoleName = model.RoleName;

                        Aliyeye.Car.Net.UI.MainWin main = new MainWin();
                        main.Tag = this.FindForm();
                        this.Visible = false;
                        MainWin.GetInstance();
                        main.ShowDialog();

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void menuBlueButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLonIn_Click(object sender, EventArgs e)
        {
            menuBlueButton1_Click(sender, e);
        }

        public static string GetOrderNumber()
        {
            return LogUserName + "_" + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second;
        }
    }
}
